package com.yaodian100.ec.activity.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.yaodian100.core.common.entity.BaseEntity;

@Entity
@Table(name = "EC_ACTIVITY_PAGE_BLOCK")
public class ActivityPageBlock extends BaseEntity {
	private static final long serialVersionUID = 5309012538192804211L;

	/**
	 * 流水號
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_ec_activity_page_block")
	@SequenceGenerator(name = "seq_ec_activity_page_block", sequenceName = "SEQ_EC_ACTIVITY_PAGE_BLOCK")
	@Column(name = "ID")
	private Long id;
	
	@Column(name = "PAGE_ID" )
	private Long pageId;
	
	//@Column(name = "BLOCK_TEMPLATE_ID" )
	//private Long blockTemplateId;
	   
	@Column(name = "TITLE", length=50)
	private String title;
	
	@Column(name = "SORT_ORDER")
	private Long sortOrder;
	
	@OneToMany(mappedBy = "blockId", targetEntity = ActivityPageBlockProduct.class)
	@LazyCollection(LazyCollectionOption.TRUE)
	@Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE_ORPHAN })
	@OrderBy("id asc")
	@NotFound(action = NotFoundAction.IGNORE)
	private List<ActivityPageBlockProduct> products = new ArrayList<ActivityPageBlockProduct>(0);
	
	@OneToMany(mappedBy = "blockId", targetEntity = ActivityPageBlockBanner.class)
	@LazyCollection(LazyCollectionOption.TRUE)
	@Cascade( { CascadeType.DELETE_ORPHAN })
	@OrderBy("itemNo asc")
	@NotFound(action = NotFoundAction.IGNORE)
	private List<ActivityPageBlockBanner> banners = new ArrayList<ActivityPageBlockBanner>(0);
	
	@OneToOne(optional = true)
	@JoinColumn(name = "BLOCK_TEMPLATE_ID", referencedColumnName = "id", unique = true)
	private ActivityPageBlockTemplate template; 

	public List<ActivityPageBlockProduct> getProducts() {
		return products;
	}

	public void setProducts(List<ActivityPageBlockProduct> products) {
		this.products.clear();
		for (ActivityPageBlockProduct product:products) {
			product.setBlockId(this.id);
			this.products.add(product);
		}
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPageId() {
		return pageId;
	}

	public void setPageId(Long pageId) {
		this.pageId = pageId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Long getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(Long sortOrder) {
		this.sortOrder = sortOrder;
	}

	public ActivityPageBlockTemplate getTemplate() {
		return template;
	}

	public void setTemplate(ActivityPageBlockTemplate template) {
		this.template = template;
	}

	public List<ActivityPageBlockBanner> getBanners() {
		return banners;
	}

	public void setBanners(List<ActivityPageBlockBanner> banners) {
		this.banners.clear();
		for (ActivityPageBlockBanner banner:banners) {
			banner.setBlockId(this.id);
			this.banners.add(banner);
		}
	}
	
	
	
}
